<template>
  <div id="flowerActiveList">
    <h1 class="con-right-title">鲜花活动</h1>
    <!--上部搜索条件组件-->
    <topSearch
      :form-inline="formInline"
      @toSearch="toSearch"
      :activeStatus="activeStatus"
    ></topSearch>
    <!-- 操作按钮部分 -->
    <div class="table-conBtn-left">
      <el-button
        class="add"
        type="success"
        icon="el-icon-plus"
        @click="addBtn('add')"
        >新增活动</el-button
      >
    </div>
    <div class="table-conBtn">
      <freshCom @searchList="searchList"></freshCom>
    </div>
    <!-- 表格部分 -->
    <el-table
      id=""
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      max-height="700"
      ref="table"
    >
      <el-table-column  width="50" label="序号"  align="center">
        <template slot-scope="scope">
          {{
            scope.$index
              | filterPage(searchParams.pageNum, searchParams.pageSize)
          }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="活动标题"></el-table-column>
      <el-table-column
        prop="activityStartTime,activityEndTime"
        label="活动时间"
        width="240"
      >
        <template slot-scope="scope">
          <span
            >{{ scope.row.activityStartTime }} <br />
            至 <br />{{ scope.row.activityEndTime }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="活动排序"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column prop="partNumber" label="参与人数" width="100"  align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.partNumber ? scope.row.partNumber : 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="workNumber" label="作品数" width="100"  align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.workNumber ? scope.row.workNumber : 0 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="totalFlowersReceived"
        label="赠送鲜花总数"
        width="130"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.totalFlowersReceived ? scope.row.totalFlowersReceived : 0
          }}</span>
        </template>
      </el-table-column>
      <!-- 鲜花活动状态 NOT_STARTED 未开始,IN_PROGRESS 进行中,CLOSED已结束 -->
      <el-table-column prop="activityStatus" label="活动状态" width="130"  align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.activityStatus === 'NOT_STARTED'">未开始</span>
          <span v-if="scope.row.activityStatus === 'IN_PROGRESS'">进行中</span>
          <span v-if="scope.row.activityStatus === 'CLOSED'">已结束</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createdTime"
        label="创建活动时间"
        width="180"
      ></el-table-column>
      <el-table-column fixed="right" label="操作" width="220">
        <template slot-scope="scope">
          <el-button
            @click="showSort(scope.row.id, scope.row.sort)"
            type="text"
          >
            编辑排序
          </el-button>
          <el-button
            v-if="scope.row.activityStatus === 'NOT_STARTED'"
            @click="addBtn('edit', scope.row.id, scope.row)"
            type="text"
          >
            编辑活动
          </el-button>
          <el-button
            v-if="scope.row.activityStatus === 'NOT_STARTED'"
            @click="handleStartActive(scope.row)"
            type="text"
          >
            开始
          </el-button>
          <el-button
            v-if="scope.row.activityStatus === 'IN_PROGRESS'"
            @click="endActive(scope.row)"
            style="color: red"
            type="text"
            >结束</el-button
          >
          <el-button
            v-if="scope.row.activityStatus != 'IN_PROGRESS'"
            @click="delActive(scope.row)"
            style="color: red"
            type="text"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.activityStatus !== 'NOT_STARTED'"
            @click="linkDataDetail(scope.row)"
            type="text"
          >
            数据明细
          </el-button>
          <el-button
            v-if="scope.row.activityStatus !== 'NOT_STARTED'"
            @click="linkPartake(scope.row)"
            type="text"
          >
            参与用户
          </el-button>
          <el-button
            v-if="scope.row.activityStatus !== 'NOT_STARTED'"
            @click="addBtn('examine', scope.row.id, scope.row)"
            type="text"
          >
            查看活动详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗部分 -->
    <dialogSort
      v-if="sortData.isShow"
      :sort-data="sortData"
      @refresh="refresh"
    ></dialogSort>

    <!--分页-全局组件-->
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
  </div>
</template>

<script>
import topSearch from './topSearch' // 上部搜索条件
import dialogSort from '@component/flowerActive/flowerActiveList/dialogSort' // 排序弹窗
import { sysNormalDisable } from '@utils/dictionariesFunc'
import {
  getFlowerActiveList,
  startActive,
  endActive,
  delActive
} from '@/api/flowerActive/flowerActiveList/index'
export default {
  name: 'flowerActiveList',
  components: {
    topSearch,
    dialogSort
    // uploadImgCom
  },
  data() {
    return {
      // 搜索条件
      formInline: {
        activityStatus: '', // 活动状态
        name: '', // 标题
        startTime: '', // 开始时间
        endTime: '', // 结束时间
        createTime: []
      },
      activeStatus: [],
      //
      loading: true,
      // 表格数据
      tableData: [],
      // 总条数
      total: 0,
      // 分页数据
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      // 弹窗变量
      sortData: {
        isShow: false, // 默认隐藏
        id: '',
        sort: '',
        total: ''
      },
      // 选择框选中的数据
      multipleSelection: []
    }
  },
  mounted() {
    /* 获取列表 */
    this.searchList()
  },
  async created() {
    await this.getActiveStatus()
    /* 获取列表 */
    this.searchList()
  },
  watch: {
    $route: 'searchList'
  },
  methods: {
    //  鲜花活动状态 字典 flowers_activity_status
    async getActiveStatus() {
      let data = await sysNormalDisable('flowers_activity_status')
      this.activeStatus = data
    },
    /* 搜索 */
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    },
    /* 刷新列表 */
    refresh() {
      this.toSearch()
    },
    /* 获取列表 */
    searchList() {
      let params = { ...this.searchParams, ...this.formInline }
      getFlowerActiveList(params, (res) => {
        if (res.rows) {
          this.tableData = res.rows
          this.total = res.total
        }
        this.loading = false
      })
    },
    // 新增方法
    addBtn(type = '', id = '', row = {}) {
      this.$router.push({
        path: '/flowerActive/flowerActiveList/addEdit',
        query: {
          type: type,
          id: id,
          row: row
        }
      })
    },
    /* 编辑排序 */
    showSort(id, sort) {
      this.sortData.isShow = true
      this.sortData.id = id
      this.sortData.sort = sort
      // 暂时用不到的参数
      this.sortData.total = this.total
    },
    // 开始活动
    handleStartActive(item) {
      this.$confirm(`确认要开始活动吗？`, '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        startActive(item, (res) => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          // 刷新列表
          this.searchList()
        })
      })
    },
    // 结束活动
    endActive(item) {
      this.$confirm(`是否结束该活动`, '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        endActive(item, (res) => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          // 刷新列表
          this.searchList()
        })
      })
    },
    // 删除活动
    delActive(item) {
      this.$confirm(`是否删除该活动`, '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delActive(item, (res) => {
          // 刷新列表
          this.searchList()
        })
      })
    },
    // 链接到数据明细
    linkDataDetail(item) {
      this.$router.push({
        path: '/flowerActive/flowerActiveList/dataDetail/index',
        query: {
          id: item.id
        }
      })
    },
    // 链接到参与用户
    linkPartake(item) {
      this.$router.push({
        path: '/flowerActive/flowerActiveList/partakeReward/index',
        query: {
          id: item.id
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.table-conBtn-left {
  .add {
    // text-align: right;
    float: left;
    margin-left: 30px;
  }
}
.table-conBtn {
}
</style>
